<?php

vendor('csv');

$file = '';
if (count($list->rows)) {

	$rows = array();

	$row = array();

	$row[] = __d($po_domain, 'created', true);
	$row[] = __d($po_domain, 'type', true);
    if (Auth::o()->is('admin')) {
    	$row[] = __d($po_domain, 'client', true);
	}
	$row[] = __d($po_domain, 'amount', true);
	$row[] = __d($po_domain, 'author', true);
	$row[] = __d($po_domain, 'candidate_name', true);
	$row[] = __d($po_domain, 'test_name', true);
	$row[] = __d($po_domain, 'report_name', true);
	$row[] = __d('clients', 'comment', true);

	$rows[] = $row;

    foreach ($list->rows as $item) {
    	$row = array();

        $row[] = Date::Short(Date::SQL2UNIX($item[$modelName]['created']));
		$row[] = h(__d($po_domain, 'type_'.$item[$modelName]['type'], true));
        if (Auth::o()->is('admin')) {
			$row[] = $item['Client']['name'];
        }
        $amount = $item[$modelName]['amount'];
        if ($item[$modelName]['type'] != 'REFILL_ACCOUNT') {
			$amount = bc::sub(0, $amount);
        }
		$row[] = $editForm->num_foTXT($amount);
		if (Auth::o()->is('client_admin')) {
			if (!empty($item['Admin']['id'])) {
				$row[] = __d($po_domain, 'admin_for_client', true);
			} elseif (!empty($item['ClientAdmin']['id'])) {
				$row[] = $item['ClientAdmin']['name'];
            } else {
                $row[] = "…";
			}
		} else {
			if (!empty($item['Admin']['id'])) {
				$row[] = $item['Admin']['username'].' ('.__d($po_domain, 'admin', true).')';
			} elseif (!empty($item['ClientAdmin']['id'])) {
             	$row[] = $item['ClientAdmin']['username'].' ('.__d($po_domain, 'client_admin', true).')';
            } else {
				$row[] = "…";
            }
	    }
	    $row[] = (!empty($item['Candidate']['id'])) ? $item['Candidate']['name'].' '.$item['Candidate']['surname'] : "—";?><?php
		$row[] = (!empty($item['Test']['id'])) ? $item['Test']['name'] : "—";
		$row[] = (!empty($item['Reporting']['Report']['id'])) ? $item['Reporting']['Report']['name'] : "—";
		$row[] = (!empty($item[$modelName]['comment'])) ? $item[$modelName]['comment'] : ($item[$modelName]['type'] == 'REFILL_ACCOUNT' ? "…" : "—");

		$rows[] = $row;

    }

    foreach ($rows as $row) {
        $file .= csv::drawLine($row);
    }
}

header("Content-Length: ".strlen($file));

echo $file;

?>